#ifndef LIST_CC
#define LIST_CC 1
#include <click/config.h>
#include "list.hh"


//namespace SNCoding
//{
//CLICK_DECLS

namespace SNCoding
{
template <typename T >
typename List<T >::iterator List<T >::push_front(const T& x) {
	if(_size>=_capcity) return iterator(0,this);
	List_member<T >* member=new List_member<T >(x);
	if ((member->_next = _head))
		_head->_prev = member;
	else{
		_head = member;
	}
	_head = member;
	_head->_prev = LIST_HEAD_MARKER;
	_size++;
	return iterator(member,this);
}

template <typename T >
typename List<T >::iterator List<T >::push_back(const T& x) {
	if(_size>=_capcity) return iterator(0,this);
	List_member<T >* member=new List_member<T >(x);
	if((member->_prev = _tail))
		_tail->_next = member;
	else{
		_head = member;
		_head->_prev = LIST_HEAD_MARKER;
	}
	_tail = member;
	_size++;
	return iterator(member,this);
}

template <typename T >
void List<T >::pop_front() {
	assert(_head);
	List_member<T >* x = _head;
	if ((_head = x->_next) != LIST_HEAD_MARKER)
		_head->_prev = LIST_HEAD_MARKER;
	else
		_head = _tail = 0;
	delete x;
	_size--;
}

template <typename T >
void List<T >::pop_back() {
	assert(_head);
	List_member<T >* x = _tail;
	if ((_tail = x->_prev) != LIST_HEAD_MARKER)
		_tail->_next = 0;
	else
		_head = _tail = 0;
	delete x;
	_size--;
}

template <typename T >
typename List<T >::iterator List<T >::insert(List_member<T >* pos, const T& x) {
	if(_size>=_capcity) return end();
	assert(pos);
	List_member<T >* member=new  List_member<T >(x);
	if ((member->_prev = pos->_prev) != LIST_HEAD_MARKER)
		member->_prev ->_next = member;
	else
		_head = member;
	member->_next = pos;
	pos->_prev=member;
	_size++;
	return iterator(member,this);
}

template <typename T >
typename List<T >::iterator List<T >::insert(iterator& it, const T& x) {
	if(_size>=_capcity) return end();
	return insert(it.get(), x);
}


/** @brief Remove @a x from the list.
 * @param x element to remove
 * @pre contains(@a x) */
template <typename T >
bool List<T >::erase(List_member<T >* x) {
	if(!x) return false;
	List_member<T >* n = x->_next, *p = x->_prev;
	if (n)
		n->_prev = p;
	else
		_tail = (p != LIST_HEAD_MARKER ? p : 0);
	if (p != LIST_HEAD_MARKER)
		p->_next = n;
	else
		_head = n;
	delete x;
	_size--;
	return true;
}
}
//CLICK_ENDDECLS
//}
//ELEMENT_PROVIDES(List)
#endif
